package com.future;

import com.future.util.TreeNode;

/**
 * Description: BM30 二叉搜索树与双向链表
 *
 * @author weiruibai.vendor
 * Date: 2022/8/1 14:18
 */
public class Solution_BM30 {


    public static void main(String[] args) {
        Solution_BM30 bm30 = new Solution_BM30();
        TreeNode head = new TreeNode(10);

        head.left = new TreeNode(6);
        head.right = new TreeNode(14);

        head.left.left = new TreeNode(4);
        head.left.right = new TreeNode(8);

        head.right.left = new TreeNode(12);
        head.right.right = new TreeNode(16);

        TreeNode convert = bm30.Convert(head);

        System.out.println();
    }

    public TreeNode Convert(TreeNode pRootOfTree) {
        if (pRootOfTree == null) {
            return null;
        }
        return process(pRootOfTree);
    }

    private TreeNode process(TreeNode pRootOfTree) {
        if (pRootOfTree == null) {
            return null;
        }
        // 左
        TreeNode left = process(pRootOfTree.left);
        // 右
        TreeNode right = process(pRootOfTree.right);
        pRootOfTree.left = left;
        if (left != null) {
            left.right = pRootOfTree;
        }
        pRootOfTree.right = right;
        if (right != null) {
            right.left = pRootOfTree;
        }
        return pRootOfTree;
    }

}
